/* flash API */
#include "flash.h"
#include "FreescaleIAP.h"
#include "baidu_iot_ota_log.h"

int flash_unlock()
{
    __disable_irq();
    return 0;
}

int flash_lock()
{
    __ISB();
    __enable_irq();

    return 0;
}

int flash_erase(unsigned int addr, unsigned int length)
{
    int index;
    IAPCode status = Success;
    for (index = 0; index < length; index += SECTOR_SIZE)
    {
        status = erase_sector(addr);
			  OTA_LOG_INFO("erase sector:%d, result:%d\n", addr, (int)status);
        addr += SECTOR_SIZE;
    }
    
    return 0;
}

int flash_write(unsigned int flash_addr, unsigned int length, unsigned int mem_addr)
{
    int index;
    /*
    for (index = 0; index < length; index += IBUF_SZ)
    {
        int chunk;

        if (index + IBUF_SZ > length)
        {
            chunk = length - index;
            memset(ibuf, 0xff, IBUF_SZ);
        }
        else chunk = IBUF_SZ;

        // copy to the ibuf, then write to the flash 
	      
        memcpy(ibuf, (void*)mem_addr, chunk);
        
        program_flash(flash_addr, (char *)ibuf, IBUF_SZ);

        flash_addr += IBUF_SZ;
        mem_addr += IBUF_SZ;
    }*/

    return 0;
}
